<?php
namespace app\admin\controller;

use app\admin\model\token;
use think\Cache;
use app\admin\api\Sites;
use app\admin\controller\Permissions;
use think\Session;
use app\admin\api\AppToken;

/**
 * 用户站点
 */
class Site extends Permissions
{
    /**
     * 创建站点
     * @return [type] [description]
     */
    public function index()
    {
        if($this->request->isPost()) {

            $post = $this->request->post();

            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['name', 'require', '站点名称不能为空'],
                ['domain', 'require', '站点域名不能为空'],
                ['description', 'require', '站点描述不能为空'],
            ]);

            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }

            //请求接口信息
            $data = new Sites();

            $res = $data->siteCreate($post);

            if ($res == 555){
                $this->success("提交成功");

            }else{
                $this->error("$res");
            }

        }else{
            return $this->fetch();
        }
    }


    /**
     * 获取网站列表
     * @return [type] [description]
     */
    public function sitelist(){
        $data = new Sites();

        $res = $data->siteList();

        $site = $res->items;

        foreach ($site as $key => $value){
            $sites[$key]['name'] = $value->name;
            $sites[$key]['uid'] = $value->uid;
        }
//        var_dump($sites);
        return $this->fetch();

    }


    /**
     * 获取站点详情
     * @return [type] [description]
     */
    public function sitedetails($id){

        //$id = 'Wd';
        $data = new Sites();
        $res = $data->siteDetails($id);

        $res = $res->item;
        $lang = $res->languages;
        $lang = implode(",", $lang);

        $app_id = $res->app->client->app_id;

        $to = new token();
        $sid = $to->where("app_id=$app_id")->value('id');

       //将站点语言存入Session
        Session::set("lang[$sid]","$lang");

        $this->assign('siteD',$res);
//        var_dump($res);
        return $this->fetch();

    }

    /**
     * 更新站点信息
     * @return [type] [description]
     */
    public function siteupdate(){

        if($this->request->isPost()) {

            $post = $this->request->post();

            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['name', 'require', '站点名称不能为空'],
                ['domain', 'require', '站点域名不能为空'],
                ['description', 'require', '站点描述不能为空'],
            ]);

            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }

            $id = $post['uid'];

            //请求接口信息
            $ss = new Sites();

            $res = $ss->siteUpdate($id,$post);

            if ($res == 555){
                $this->success("修改成功");

            }else{
                $this->error("$res");
            }

        }

    }

    /**
     * 删除站点
     * @return [type] [description]
     */
    public function sitedelete()
    {

        if ($this->request->isPost()) {

            //接收数据
            $post = $this->request->post();
            $id = $post['uid'];

            //请求接口
            $data = new Sites();
            $res = $data->siteDelete($id);

            if ($res == 555) {
                $this->success("撤销成功");

            } else {
                $this->error("$res");
            }

        }
    }

    public function test(){

//        $id = 'v5';
        $data['name'] = 'xx国际交流会议';
        $data['domain'] = 'http://www.xx.com';
        $data['description'] = '欢迎大就能查擦家来看学习大会';
        $data['languages'][0] = 'zh-CN';
//        $data['languages'][1] = 'zh-CN';

//        $id = 'Wd';
        $data1 = new Sites();
        $res = $data1->siteCreate($data);

//        $res = $res->item;

        //var_dump($res);

        var_dump($res);

    }



}
